import numpy as np
import matplotlib.pyplot as plt
from python_ai.ML.lin_regression.xlib import *

data_loaded = np.loadtxt(r'../data/ex1data1.txt', delimiter=',')
m = len(data_loaded)
n = 2
print(m)
print(data_loaded[:5])
x = data_loaded[:, 0].reshape(m, 1)
y = data_loaded[:, 1].reshape(m, 1)
print(x[:5])
print(y[:5])

X = np.c_[np.ones([m, 1]), x]
theta = regular_equation_algorithm(X, y)
xscore = P_score_function(X, y, theta)
print('regular equation')
print(f'THETA = {theta}')
print(f'score = {xscore}')

plt_line_x = np.array([x.min(), x.max()]).ravel()
plt_line_y = np.c_[np.ones([2, 1]), plt_line_x].dot(theta).ravel()


plt_data_x = x.ravel()
plt_data_y = y.ravel()
plt.scatter(plt_data_x, plt_data_y)
plt.plot(plt_line_x, plt_line_y, 'r-')
plt.show()

